package com.dap.batch.controller;

import com.dap.batch.dao.IDaoService;
import com.dap.batch.entity.Result;
import com.dap.batch.enums.ResultEnum;
import com.dap.batch.exception.BatchCustomException;
import com.dap.batch.po.BatchJobExecutionPo;
import com.dap.batch.service.IBatchJobService;
import com.dap.batch.utils.ResultUtils;
import com.dap.dao.model.QueryResult;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2017/9/28 15:59
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/9/28 15:59
 */
@RestController
@RequestMapping("/v1/job")
public class BatchJobMonitorController {

    private final static Logger logger = LoggerFactory.getLogger(BatchJobMonitorController.class);

    private IDaoService daoService;
    private IBatchJobService jobService;

    @Autowired
    public BatchJobMonitorController(IDaoService daoService, IBatchJobService jobService) {
        this.daoService = daoService;
        this.jobService = jobService;
    }

    /**
     * 查询所有一个任务组下所有任务监控数据
     *
     * @param groupExeId
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "获取当前任务组下任务监控信息", httpMethod = "GET")
    @GetMapping("/monitors/{groupExeId}")
    public Result list(@PathVariable Long groupExeId
            , @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit) {
        BatchJobExecutionPo po = new BatchJobExecutionPo();
        po.setGroupExeId(groupExeId);
        QueryResult<BatchJobExecutionPo> result = daoService.selectQueryResult(po,
                page, limit);
        if (result.getTotalrecord() == 0) {
            throw new BatchCustomException(ResultEnum.NOT_FOUND);
        }
        return ResultUtils.success(result);
    }

    /**
     * 修改执行参数
     * @param po
     * @return
     */
    @ApiOperation(value = "修改任务监控执行参数", httpMethod = "PUT")
    @PutMapping("/monitor")
    public Result update(BatchJobExecutionPo po) {
        int i = daoService.update(po);
        if (i == 0) {
            throw new BatchCustomException(ResultEnum.NOT_FOUND);
        }
        return ResultUtils.success(ResultEnum.CREATED);
    }

    /**
     * 续跑任务
     * @param exeId
     * @return
     */
    @ApiOperation(value = "续跑任务", httpMethod = "POST")
    @PostMapping("/goon/{exeId}")
    public Result goon(@PathVariable Long exeId) {
        try {
            jobService.goon(exeId);
            return ResultUtils.success(ResultEnum.CREATED);
        } catch (Exception e) {
            logger.error("续跑失败", e);
            throw new BatchCustomException(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 重跑任务
     * @param exeId
     * @return
     */
    @ApiOperation(value = "重跑任务", httpMethod = "POST")
    @PostMapping("/redo/{exeId}")
    public Result redo(@PathVariable Long exeId) {
        try {
            jobService.redo(exeId);
            return ResultUtils.success(ResultEnum.CREATED);
        } catch (Exception e) {
            logger.error("重跑失败", e);
            throw new BatchCustomException(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 取消任务执行
     * @param exeId
     * @param force 1:强制 0:不强制
     * @return
     */
    @ApiOperation(value = "停止任务", httpMethod = "POST")
    @PostMapping("/cancel/{exeId}")
    public Result cancel(@PathVariable Long exeId,@RequestParam() Integer force) {
        try {
            jobService.cancel(exeId, false, force==1);
            return ResultUtils.success(ResultEnum.CREATED);
        } catch (Exception e) {
            logger.error("停止失败", e);
            throw new BatchCustomException(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }
}
